//
//  NumberInputView.m
//  PurchasingMeeting
//
//  Created by zhuang yihang on 7/10/13.
//  Copyright (c) 2013 YX. All rights reserved.
//

#import "NumberInputView.h"

@interface NumberInputView(){
    NSString *value_;
}

@end

@implementation NumberInputView
@synthesize delegate = delegate_;
@synthesize value = value_;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(clear:) name:@"clear" object:nil];
#ifdef DHH_iOS
        int col = 4;
        int row = 3;
#else
        int col = 3;
        int row = 4;
#endif
        float interval = 10;
        
        float buttonWidth = (self.width-(col+1)*interval)/col;
        float buttonHeight = (self.height-(row+1)*interval)/row;
        
        for (int i = 0; i < col * row; i++) {
            UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
            [self addSubview:b];
            
            int r = i / col;
            int c = i % col;
            
            b.tag = i;
            
            b.frame = CGRectMake(interval + c * (buttonWidth + interval), interval + r *(buttonHeight + interval),
                                 buttonWidth, buttonHeight);
          //  b.backgroundColor = [UIColor whiteColor];
            [b setBackgroundImage:[[UIHelper getInstance] getBundleImage:@"anniu.png"] forState:UIControlStateNormal];
            if (i<9) {
                NSString *str = [NSString stringWithFormat:@"%d",i+1];
                [b setTitle:str forState:UIControlStateNormal];
            }
            else if(i==9)
            {
                NSString *str = [NSString stringWithFormat:@"C"];
                [b setTitle:str forState:UIControlStateNormal];
            }
            else if(i==10)
            {
                NSString *str = [NSString stringWithFormat:@"0"];
                [b setTitle:str forState:UIControlStateNormal];
            }
            else if(i==11)
            {
                NSString *str = [NSString stringWithFormat:@"→"];
                [b setTitle:str forState:UIControlStateNormal];
            }
            b.titleLabel.font = [UIFont systemFontOfSize:40];
            /*
            if (i==0) {
                [b setBackgroundImage:[[UIHelper getInstance] getBundleImage:@"1.png"] forState:UIControlStateNormal];
            }
            else if(i==1)
            {
                [b setBackgroundImage:[[UIHelper getInstance] getBundleImage:@"2.png"] forState:UIControlStateNormal];
            }
            else if(i==2)
            {
                [b setBackgroundImage:[[UIHelper getInstance] getBundleImage:@"3.png"] forState:UIControlStateNormal];
            }
            else if(i==3)
            {
                [b setBackgroundImage:[[UIHelper getInstance] getBundleImage:@"4.png"] forState:UIControlStateNormal];
            }
            else if(i==4)
            {
                [b setBackgroundImage:[[UIHelper getInstance] getBundleImage:@"5.png"] forState:UIControlStateNormal];
            }
            else if(i==5)
            {
                [b setBackgroundImage:[[UIHelper getInstance] getBundleImage:@"6.png"] forState:UIControlStateNormal];
            }
            else if(i==6)
            {
                [b setBackgroundImage:[[UIHelper getInstance] getBundleImage:@"7.png"] forState:UIControlStateNormal];
            }
            else if(i==7)
            {
                [b setBackgroundImage:[[UIHelper getInstance] getBundleImage:@"8.png"] forState:UIControlStateNormal];
            }
            else if(i==8)
            {
                [b setBackgroundImage:[[UIHelper getInstance] getBundleImage:@"9.png"] forState:UIControlStateNormal];
            }
            else if(i==9)
            {
                [b setBackgroundImage:[[UIHelper getInstance] getBundleImage:@"c.png"] forState:UIControlStateNormal];
            }
            else if(i==10)
            {
                [b setBackgroundImage:[[UIHelper getInstance] getBundleImage:@"0.png"] forState:UIControlStateNormal];
            }
            else if(i==11)
            {
                [b setBackgroundImage:[[UIHelper getInstance] getBundleImage:@"jt.png"] forState:UIControlStateNormal];
            }
            */
            [b addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/


- (void)clicked:(UIButton *)sender{
    if (value_==nil) {
        value_ = @"";
    }
    
    if (sender.tag <=8  || sender.tag == 10) {
        NSString *s = [NSString stringWithFormat:@"%d",sender.tag+1];
        if (sender.tag == 10) {
            s = @"0";
        }
        else if(sender.tag == 0)
        {
            s = @"1";
        }
        else if(sender.tag == 1)
        {
            s = @"2";
        }
        else if(sender.tag == 2)
        {
            s = @"3";
        }
        else if(sender.tag == 3)
        {
            s = @"4";
        }
        else if(sender.tag == 4)
        {
            s = @"5";
        }
        else if(sender.tag == 5)
        {
            s = @"6";
        }
        else if(sender.tag == 6)
        {
            s = @"7";
        }
        else if(sender.tag == 7)
        {
            s = @"8";
        }
        else if(sender.tag == 8)
        {
            s = @"9";
        }
        
        if (!([value_ intValue]==0 && [s intValue]==0)) {
            value_ = [value_ stringByAppendingString:s];
        }
        
        
    }else if(sender.tag == 9){
        value_ = @"";
    }else if(sender.tag == 11){
        [[NSNotificationCenter defaultCenter]postNotificationName:@"search" object:nil];
        
        [delegate_ numberInputViewEnterClick:value_];
        return;
       // value_ = @"-1";
    }
    
//    
//    if (value_.length==0) {
//        value_ = @"0";
//    }
    
    [delegate_ numberInputViewDidClick:value_];
}
-(void)clear:(NSNotification *)sender
{
    NSString *str = sender.object;
    value_ = str;
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

@end
